
<template>
  <div class="tenantMenu_main main_container user_container ">
    <div v-loading="loading" class="main_main">
      <div class="SearchBar_main main_main_search">
        <div class="main_main_search_item">
          <el-input v-model="pageData.searchValue" class="search_input" placeholder="请输入物料编码或名称" clearable @keyup.enter.native="getList" @clear="getList" />
          <el-button class="button_icon" icon="el-icon-search" @click.stop="getList" />
        </div>
      </div>
      <div ref="table_container" style="margin-top:20px" class="table_container">
        <el-table
          ref="table"
          :data="tableData"
          stripe
          border
          :height="tableHeight"

          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
          @sort-change="(column)=>{sortChange(column, this)}"
          @selection-change="(val)=>{selectionChange(val, this)}"
        >
          <el-table-column type="selection" width="70" align="center" />
          <Column
            v-for="(column, index) in defaultTableColumns"
            :key="index"
            :prop="column.prop"
            :is-show="column.isShow"
            :label="column.label"
            :sortable="column.sortable"
            :width="column.width"
          />
          <el-table-column label="备注" width="200">
            <template slot-scope="scope">
              <el-input v-model="scope.row.remark" placeholder="请输入" clearable />
            </template>
          </el-table-column>
          <el-table-column label="送检数量" min-width="100">
            <template slot-scope="scope">
              <el-input v-model="scope.row.inspectNumber" placeholder="请输入" clearable />
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="main_bottom">
        <pagination
          :total="total"
          :page.sync="pageData.pageNum"
          :limit.sync="pageData.pageSize"
          :selected-num="selectionsAll.length?selectionsAll.length:0"
          @pagination="getList"
        />
      </div>
    </div>
    <div style="background:white;border-top:1px solid rgba(51, 51, 51, 0.5)" class="main_bottom">
      <el-button type="cancel" @click.stop="cancel">取消</el-button>
      <el-button @click.stop="onSubmit">保存</el-button>
    </div>
  </div>
</template>

<script>
import { getList } from '@/api/outsourceManage/outsourceOrder'
import AddOrUpdate from './addOrUpdate' // 新增或编辑页面

export default {
  props: {
    layerid: {},
    id: {},
    selectedRow: {}
  },
  data() {
    return {
      materialTypeId: '',
      typeList: [],
      activeNum: 1,
      deptData: [],
      oldTableData: [],
      currentDeptId: '',
      currentDeptName: '',
      loading: false,
      pageId: '用户管理', // 页面唯一标识
      pageData: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: 'updateTime',
        isAsc: 'desc',
        searchValue: ''
      },
      total: 1, // 数据一共有多少条
      mainId: 'outsourceOrderDetailId', // 表格行ID
      tableHeight: '0', // 表格高度
      tableData: [], // 表格数据
      tableColumns: [], // 表格实际用的字段
      searchColumns: [], // 搜索区实际用的字段
      selections: [], // 表格当前页所选中的行
      selectionsAll: [], // 表格所有页选中的行
      peopleList: [],
      tableDataOne: [],
      defaultTableColumns: [
        // 表格常规字段（初始值）
        { prop: 'materialCode', label: '物料编码' },
        { prop: 'materialName', label: '物料名称' },
        { prop: 'materialModel', label: '规格型号' },
        { prop: 'materialUnitName', label: '物料单位' },
        { prop: 'outsourceNumber', label: '外协数量' }
      ],

      operationData: [
        // 右上角的操作按钮
        { label: '导入', perm: 'system:user:import' },
        { label: '新增', perm: 'system:user:add' },
        { label: '批量删除', perm: 'system:user:delete' },
        { label: '批量导出', perm: 'system:user:export' },
        { label: '页面配置', perm: '' }
      ],
      defaultProps: {
        label: 'name',
        children: 'children'
      }
    }
  },
  mounted() {
    // this.getMaterialTypeList()
    this.getList()
    this.oldTableData = this.$deepClone(this.selectedRow)
    this.selectionsAll = this.selectedRow
    this.$nextTick(() => {
      this.tableHeight = this.$refs.table_container.offsetHeight
    })
  },
  methods: {
    // getMaterialTypeList() {
    //   list(this.pageData).then(res => {
    //     this.typeList.push({ name: '全部', id: '', children: [] })
    //     this.typeList[0].children = res.data
    //   })
    // },
    getList() {
      if (this.tableData.length > 0) {
        this.oldTableData = this.$deepClone(this.tableData)
      }
      getList({ ...this.pageData, outsourceOrderId: this.id }).then(res => {
        this.tableData = res.data
        this.total = res.total
        this.tableData.filter(item => {
          item.remark = ''
          item.outsourceOrderDetailId = item.id
          item.id = ''
          delete item.id
        })
        this.tableData = this.selectedRow.length > 0 ? this.mergeById(res.data, this.selectedRow) : res.data
        this.$nextTick(() => {
          this.setSelectRow(this)// 设置行选中
        })
      })
    },
    mergeById(arr1, arr2) {
      const mergedArr = [...arr1, ...arr2]
      const resultMap = mergedArr.reduce((result, item) => {
        const { outsourceOrderDetailId } = item
        if (!result[outsourceOrderDetailId]) {
          result[outsourceOrderDetailId] = { ...item }
        } else {
          result[outsourceOrderDetailId] = { ...result[outsourceOrderDetailId], ...item }
        }
        return result
      }, {})

      return Object.values(resultMap)
    },
    add() {
      this.$layer.iframe({
        // 弹框
        content: {
          content: AddOrUpdate, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {} // props
        },
        area: ['880px', '700px'],
        title: '新增物料信息', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    cancel() {
      this.$layer.close(this.layerid)
    },
    onSubmit() {
      // this.$parent.showMaterial(this.selectionsAll)
      // this.$layer.close(this.layerid)
      if (this.selections.filter(item => !item.inspectNumber).length > 0) {
        this.$message.warning('送检数量不能为空')
        return
      } else if (this.selections.filter(item => item.inspectNumber && !this.SETTINGS.reg_no_zero.test(item.inspectNumber)).length > 0) {
        this.$message.warning('送检数量未正确填写')
        return
      }
      this.$parent.showMaterial(this.selections)
      this.$layer.close(this.layerid)// 关闭弹框
    },
    Search() {
      const arr = JSON.parse(JSON.stringify(this.selectionsAll))
      this.tableData = this.tableDataOne.filter(item => item.materialCode.indexOf(this.pageData.searchValue) !== -1 || item.materialName.indexOf(this.pageData.searchValue) !== -1)
      this.selectionsAll = arr
      this.$nextTick(() => {
        this.setSelectRow(this)// 设置行选中
      })
    },
    SearchList() {
      if (!this.pageData.searchValue) {
        this.getList()
      }
    }
  }
}
</script>

<style lang="less" scoped>
/deep/.custom-row-style:hover {
  background-color: transparent !important; /* 取消hover高亮 */
}
/deep/ .el-input__suffix{
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  height: 36px !important;
}
/deep/ .el-input__inner{
  text-align: unset !important;
}
.button{
  width: 100px;
  height: 30px;
  color: #fff;
  background-color: #387bfc;
  border-radius: 4px;
  text-align: center;
  line-height: 30px;
}
.formItem_header_operate_items{
  height: 30px;
  display: flex;justify-content: center;align-items: center;
  padding: 0px 10px;font-size: 16px;border-radius: 4px;
  color: rgba(56, 123, 252, 1);background-color: rgba(235, 242, 255, 1);
  .item_name{
    font-size: 14px;padding-left: 10px;
  }
}
.formItem_header_operate_items:hover{
  background-color: rgba(235, 242, 255, 0.5);
}

.table_container{
  margin-top: 20px !important;
}
.addtype-main_bottoms{
  padding-right: 20px !important;
  margin: 10px 0;
}
.main_search_item{
  padding-right: 20px !important;
}
.main_main_search{
  padding-right: 20px !important;
}
.main_main_search_item{
  padding-left: 20px !important;
}
.main_main{
  margin: 0 !important;
  padding-top:0 !important;
  height: calc(100% - 50px) !important;

}
.button_icon{
  background:#387bfc;
  width:30px;
}
.search_input{
  width:200px;
  margin-right:10px
}
/deep/ .el-table--enable-row-hover .el-table__body tr:hover > td {
  background-color: rgba(235, 242, 255, 0.5);
}
</style>
